﻿using System;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;

namespace Gmantis.Controls
{
    internal class ContextMenuItemBehavior : MenuItemNonFirstLevel
    {
        public ContextMenuItemBehavior(MenuItem item)
            : base(item)
        {
        }

        public override MenuItem GetFirstItem()
        {
            return base.GetNextEnabledItem(base.MenuItem.GetParentContainers<MenuItem>(), 0);
        }

        public override MenuItem GetLastItem()
        {
            return base.GetPrevEnabledItem(base.MenuItem.GetParentContainers<MenuItem>(), base.MenuItem.GetParentContainers<MenuItem>().Count - 1);
        }

        public override MenuItem GetNextItem(Orientation orientation)
        {
            MenuItem nextEnabledItem = null;
            switch (orientation)
            {
                case Orientation.Horizontal:
                    if (base.MenuItem.HasItems)
                    {
                        nextEnabledItem = base.GetNextEnabledItem(base.MenuItem.MenuItems, 0);
                        if ((nextEnabledItem == null) && !base.MenuItem.IsSubmenuOpen)
                        {
                            CancelSourceEventArgs e = new CancelSourceEventArgs
                            {
                                Source = base.MenuItem,
                                Cancel = false
                            };
                            base.MenuItem.OnSubmenuOpening(e);
                        }
                    }
                    return (nextEnabledItem ?? base.MenuItem);
            }
            if (base.MenuItem.IsSubmenuOpen && base.MenuItem.HasItems)
            {
                return (base.GetNextEnabledItem(base.MenuItem.MenuItems, 0) ?? base.MenuItem);
            }
            IList<MenuItem> parentContainers = base.MenuItem.GetParentContainers<MenuItem>();
            return (base.GetNextEnabledItem(parentContainers, (base.MenuItem.Index + 1) % parentContainers.Count) ?? base.MenuItem);
        }

        public override MenuItem GetPrevItem(Orientation orientation)
        {
            switch (orientation)
            {
                case Orientation.Horizontal:
                    {
                        MenuItem menuItem = base.MenuItem;
                        if (!base.MenuItem.IsSubmenuOpen)
                        {
                            if (!base.MenuItem.IsFirstLevel)
                            {
                                menuItem = base.MenuItem.ParentItem;
                            }
                            return menuItem;
                        }
                        CancelSourceEventArgs e = new CancelSourceEventArgs
                        {
                            Source = base.MenuItem,
                            Cancel = false
                        };
                        base.MenuItem.OnSubmenuClosing(e);
                        return menuItem;
                    }
            }
            if (base.MenuItem.IsSubmenuOpen && base.MenuItem.HasItems)
            {
                return (base.GetPrevEnabledItem(base.MenuItem.MenuItems, base.MenuItem.MenuItems.Count - 1) ?? base.MenuItem);
            }
            IList<MenuItem> parentContainers = base.MenuItem.GetParentContainers<MenuItem>();
            return (base.GetPrevEnabledItem(parentContainers, (base.MenuItem.Index == 0) ? (parentContainers.Count - 1) : (base.MenuItem.Index - 1)) ?? base.MenuItem);
        }

        public override void MakeRelative(Popup popup)
        {
            popup.MakeRelative(base.MenuItem, new Func<List<AnchorMatching>>(this.GetPreferedLocations), null, null, false);
        }
    }
}
